﻿using System;
using System.Windows.Forms;
using AnimaGestionPersonnage.Utilities;
using AnimaGestionPersonnage.Properties;
using System.Collections;

namespace AnimaGestionPersonnage.Vue
{
    public partial class FormCreateCombatModule : Form
    {
        private bool saved = true;
        private CombatModule thisModule = new CombatModule();

        public FormCreateCombatModule()
        {
            InitializeComponent();
        }

        private void FormCreateCombatModule_Load(object sender, EventArgs e)
        {
            this.comboBoxTypeSkill.Items.AddRange(new ArrayList(Settings.Default.AllSkillTypes).ToArray());
            this.thisModule.cost = 10;
            this.thisModule.canBeMultiple = false;
        }

        private void textBoxName_TextChanged(object sender, EventArgs e)
        {
            this.thisModule.name = this.textBoxName.Text;
            this.dataChanged();
        }

        private void numericUpDownCost_ValueChanged(object sender, EventArgs e)
        {
            this.thisModule.cost = (int)numericUpDownCost.Value;
            this.dataChanged();
        }

        private void richTextBoxDescription_TextChanged(object sender, EventArgs e)
        {
            this.thisModule.description = this.richTextBoxDescription.Text;
            this.dataChanged();
        }

        private void comboBoxTypeSkill_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.thisModule.type = (TypeSkill)this.comboBoxTypeSkill.SelectedIndex;
            this.dataChanged();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (Utils.combatModuleList.Contains(this.thisModule))
            {
                Utils.combatModuleList.Remove(this.thisModule);
            }
            Utils.combatModuleList.Add(this.thisModule);
            Utils.weaponModuleList.Sort();
            Utils.xmlSerializeCombatModules(Settings.Default.FilePath + Settings.Default.CombatModule);
            this.saved = true;
            this.buttonSave.Enabled = false;
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            this.thisModule = new FormEditionSelection().ShowDialog(this);
            if (this.thisModule != null)
            {
                this.loadModule();
                this.saved = true;
                this.buttonSave.Enabled = false;
            }
        }

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormCreateCombatModule_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = Utils.onCloseEvent(saved, this, null,e);
        }

        #region local methods

        private void dataChanged()
        {
            if (Utils.getCurrentState() != Utils.LOADING_STATE)
            {
                this.buttonSave.Enabled = true;
                this.saved = false;
            }
        }

        private void loadModule()
        {
            int tempState = Utils.getCurrentState();

            Utils.setCurrentState(Utils.LOADING_STATE);

            this.numericUpDownCost.Value = this.thisModule.cost;
            this.richTextBoxDescription.Text = this.thisModule.description;
            this.textBoxName.Text = this.thisModule.name;
            this.comboBoxTypeSkill.SelectedIndex = (int)this.thisModule.type;
            this.checkBoxMultiple.Checked = this.thisModule.canBeMultiple;

            Utils.setCurrentState(tempState);
        }

        #endregion

        private void checkBoxMultiple_CheckedChanged(object sender, EventArgs e)
        {
            this.thisModule.canBeMultiple = this.checkBoxMultiple.Checked;
        }
    }
}
